﻿using CommonUtils;
using Test.Core.Web.Entities;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace Test.Core.Web.Controllers
{
    [SwaggerTag("用户")]
    public class UserController : DbController<Db, User>
    {
        [HttpGet]
        [SwaggerOperation("单项查询")]
        public ApiResult<User> Query(
            [SwaggerParameter("主键ID")] string? id,
            [SwaggerParameter("账号")] string? account,
            [SwaggerParameter("密码")] string? password,
            [SwaggerParameter("PasswordConfirmed")] string? passwordConfirmed,
            [SwaggerParameter("Name")] string? name,
            [SwaggerParameter("Email")] string? email)
        {
            var query = NewQuery()
            .And(id.IsNotEmpty(), m => m.Id == id)
            .And(account.IsNotEmpty(), m => m.Account == account)
            .And(password.IsNotEmpty(), m => m.Password == password)
            .And(passwordConfirmed.IsNotEmpty(), m => m.PasswordConfirmed == passwordConfirmed)
            .And(name.IsNotEmpty(), m => m.Name == name)
            .And(email.IsNotEmpty(), m => m.Email == email);
            return ApiResult.Ok(Db.Query(query));
        }

        [HttpGet]
        [SwaggerOperation("列表查询")]
        public ApiResult<User[]> List(
            [SwaggerParameter("账号")] string? account,
            [SwaggerParameter("密码")] string? password,
            [SwaggerParameter("PasswordConfirmed")] string? passwordConfirmed,
            [SwaggerParameter("Name")] string? name,
            [SwaggerParameter("Email")] string? email)
        {
            var query = NewQuery()
            .And(account.IsNotEmpty(), m => m.Account == account)
            .And(password.IsNotEmpty(), m => m.Password == password)
            .And(passwordConfirmed.IsNotEmpty(), m => m.PasswordConfirmed == passwordConfirmed)
            .And(name.IsNotEmpty(), m => m.Name == name)
            .And(email.IsNotEmpty(), m => m.Email == email);
            return ApiResult.Ok(Db.List(query));
        }

        [HttpGet]
        [SwaggerOperation("分页查询")]
        public ApiResult<Page<User>> Page(
            [SwaggerParameter("账号")] string? account,
            [SwaggerParameter("密码")] string? password,
            [SwaggerParameter("PasswordConfirmed")] string? passwordConfirmed,
            [SwaggerParameter("Name")] string? name,
            [SwaggerParameter("Email")] string? email,
            [SwaggerParameter("页码")] int? pageNum,
            [SwaggerParameter("页容")] int? pageSize)
        {
            var query = NewQuery()
            .And(account.IsNotEmpty(), m => m.Account == account)
            .And(password.IsNotEmpty(), m => m.Password == password)
            .And(passwordConfirmed.IsNotEmpty(), m => m.PasswordConfirmed == passwordConfirmed)
            .And(name.IsNotEmpty(), m => m.Name == name)
            .And(email.IsNotEmpty(), m => m.Email == email);
            return ApiResult.Ok(Db.Page(pageNum, pageSize, query));
        }
    }
}
